# $Id: $


package Zuz::Action;

use strict;
use warnings;

use Dancer ':syntax';
use Zuz::Sessions;


sub is_auth_required {
	my $self = shift;

	return 1;
}


sub user_type {
	my $self = shift;

	return 'unknown';
}

sub new {
	my $class = shift;

	my $self = {};
	return bless $self, $class;
}


sub process {
	my $self = shift;
	$self->{'session'}   = $self->get_session();
	$self->{'user_type'} = $self->get_user_type();

	if ( $self->is_auth_required() && (not $self->is_user_logged_in()) ) {
		debug "WE ARE IN LOGIN!";
		redirect '/login';
	}
	elsif ( ($self->user_type() ne 'unknown') && ($self->user_type() ne $self->{'user_type'}) ) {
		return "Error Access! Fuck off!";
	}
	else {
		$self->generate_content();
	}
}


sub generate_content {
	my $self = shift;
	return "DEFAULT CONTENT!";
}


sub get_session {
	my $self = shift;
	my $session = session->{zuz};
	return if !$session;

	my %data = ('session_id' => session->{zuz});
	$session = Zuz::Sessions->retrieve(%data);
	return ( $session->id ) ? $session : 0;
	
}


sub get_user_type {
	my $self = shift;
	
	return 'unknown' if not $self->{'session'};
	return $self->{'session'}->user_id->user_type;
}


sub get_username {
	my $self = shift;

	return 'unknown' if not $self->{'session'};
	return $self->{'session'}->user_id->firstname;
}


sub is_user_logged_in {
	my $self = shift;

	return $self->{'session'} ? 1 : 0;
}

# override these methods


1;